package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.TransLimitDao;
import com.jiepos.mpos.common.entity.App_Trans_LimitEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 交易限额表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月30日上午9:43:44
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class TransLimitService extends BaseService {
	
	@Autowired
	private TransLimitDao transLimitDao;
	
	
	/**
	 * 查询交易限额列表json分页数据
	 * @param pagination
	 * @param transLimit
	 * @return
	 */
	public Map getTransLimitList(Pagination pagination,App_Trans_LimitEntity transLimit){		
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.limit_uuid as uuid,NVL2 ( t2.code_val||t2.code_desc, t2.code_val||'-'||t2.code_desc, '') as limittype,")
		.append("t1.limit_idx,to_char(t1.limit_amount,'fm99999999999990.00') as limit_amount,")
		.append("NVL2 ( t1.begdate,substr( t1.begdate,1 , 4 )||'-'||substr( t1.begdate,5, 2 )||'-'||substr( t1.begdate,7 , 2), '') as begdate,")
		.append("NVL2 ( t1.begtime,substr( t1.begtime,1 , 2 )||':'||substr( t1.begtime,3, 2 )||':'||substr( t1.begtime,5 , 2), '') as begtime,")
		.append("NVL2 ( t1.enddate,substr( t1.enddate,1 , 4 )||'-'||substr( t1.enddate,5, 2 )||'-'||substr( t1.enddate,7 , 2), '') as enddate,")
		.append("NVL2 ( t1.endtime,substr( t1.endtime,1 , 2 )||':'||substr( t1.endtime,3, 2 )||':'||substr( t1.endtime,5 , 2), '') as endtime,")
		.append("NVL2 ( t1.trans_date,substr( t1.trans_date,1 , 4 )||'-'||substr( t1.trans_date,5, 2 )||'-'||substr( t1.trans_date,7 , 2), '') as trans_date,")
		.append("to_char(t1.trans_amount,'fm99999999999990.00') as trans_amount,")
		.append("NVL2 ( t3.channel_no||t3.chn_nm, t3.channel_no||'-'||t3.chn_nm, '') as channelno,")
		.append("NVL2 (t4.code_val||t4.code_desc , t4.code_val||'-'||t4.code_desc , '') as limitst from ")
		.append(Table.T_APP_TRANS_LIMIT)
		.append(" t1 left join ")
		.append(Table.T_APP_CODE_LIST)
		.append(" t2 on t1.limit_type=t2.code_val and t2.code_tp='LIMIT_TYPE' ")
		.append(" left join ")
		.append(Table.T_APP_INF_CHANNEL)
		.append(" t3 on t1.channel_no=t3.channel_no  ")
		.append(" left join ")
		.append(Table.T_APP_CODE_LIST)
		.append(" t4 on t1.limit_st=t4.code_val and t4.code_tp='LIMIT_ST' ")
		.append(" where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(transLimit.getLimit_type())) {
			sb.append( " and t1.limit_type = ? " );
			list.add(transLimit.getLimit_type());
		}
		if (!StringUtils.isBlank(transLimit.getLimit_idx())) {
			sb.append( " and t1.limit_idx like ? " );
			list.add("%" + transLimit.getLimit_idx() + "%");
		}
		if (!StringUtils.isBlank(transLimit.getChannel_no())) {
			sb.append( " and t1.channel_no like ? " );
			list.add("%" + transLimit.getChannel_no() + "%");
		}
		if (!StringUtils.isBlank(transLimit.getLimit_st())) {
			sb.append( " and t1.limit_st = ? " );
			list.add(transLimit.getLimit_st());
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return transLimitDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 保存交易限额
	 * @param transLimit
	 * @param map
	 */
	public void saveTransLimit(App_Trans_LimitEntity transLimit,Map map){
		transLimit.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		transLimit.setBegdate(transLimit.getBegdate().replace("-", ""));
		transLimit.setEnddate(transLimit.getEnddate().replace("-", ""));
		transLimit.setBegtime(transLimit.getBegtime().replace(":", ""));
		transLimit.setEndtime(transLimit.getEndtime().replace(":", ""));		
		if (!StringUtils.isBlank(transLimit.getLimit_uuid())) {
			App_Trans_LimitEntity newTransLimit=transLimitDao.findByParam("limit_uuid", transLimit.getLimit_uuid());
			transLimit.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			transLimit.setRec_id(newTransLimit.getRec_id());
			transLimit.setLast_oper_in(SystemConstant.OPER_UPDATE);			
			List noChangeList = new ArrayList();
			noChangeList.add("limit_uuid");
			noChangeList.add("rec_crt_ts");
			noChangeList.add("trans_date");
			noChangeList.add("trans_amount");
			transLimitDao.update(transLimit,noChangeList);
		}else{
			transLimit.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			transLimit.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			transLimit.setLimit_uuid(IDFactoryUtil.getUUID());			
			transLimit.setRec_st(SystemConstant.NORMAL_STATUS);
			transLimit.setLast_oper_in(SystemConstant.OPER_ADD);
			transLimitDao.save(transLimit);
		}
	}
	
	
	/**
	 * 根据记录id查询实体对象
	 * @param limit_uuid
	 * @return
	 */
	public Map getTransLimitById(String limit_uuid){
		if (StringUtils.isBlank(limit_uuid) || limit_uuid.equals("undefined")) {
			return new HashMap();
		}
		StringBuilder sb = new StringBuilder();				
		sb.append("select t1.limit_uuid,t1.limit_type,t1.limit_idx,to_char(t1.limit_amount,'fm99999999999990.00') as limit_amount,")
		.append("NVL2 ( t1.begdate,substr( t1.begdate,1 , 4 )||'-'||substr( t1.begdate,5, 2 )||'-'||substr( t1.begdate,7 , 2), '') as begdate,")
		.append("NVL2 ( t1.begtime,substr( t1.begtime,1 , 2 )||':'||substr( t1.begtime,3, 2 )||':'||substr( t1.begtime,5 , 2), '') as begtime,")
		.append("NVL2 ( t1.enddate,substr( t1.enddate,1 , 4 )||'-'||substr( t1.enddate,5, 2 )||'-'||substr( t1.enddate,7 , 2), '') as enddate,")
		.append("NVL2 ( t1.endtime,substr( t1.endtime,1 , 2 )||':'||substr( t1.endtime,3, 2 )||':'||substr( t1.endtime,5 , 2), '') as endtime,")		
		.append("t1.channel_no,")
		.append("NVL2 ( t2.channel_no||t2.chn_nm,t2.channel_no||'-'||t2.chn_nm, '')  as channelno,t1.limit_st from ");
		sb.append(Table.T_APP_TRANS_LIMIT);
		sb.append(" t1 left join ");				
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t2 on t1.channel_no=t2.channel_no  ");				
		sb.append(" where t1.rec_st!='");
		sb.append(SystemConstant.DEL_STATUS);
		sb.append("' and t1.limit_uuid='");
		sb.append(limit_uuid+"'");
		return transLimitDao.queryForMap(sb.toString());
	}
	
	
	/**
	 * 根据记录id集合删除数据
	 * @param limit_uuids
	 */
	public void deleteTransLimitByIds(String limit_uuids) {
		transLimitDao.deleteByStatus("limit_uuid",limit_uuids);
	}
	
	/**
	 * 校验交易限额
	 * @param transLimit
	 * @return
	 */
	public String checkTransLimit(App_Trans_LimitEntity transLimit){
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.limit_uuid,t1.limit_type,t1.limit_idx from ");		
		sb.append(Table.T_APP_TRANS_LIMIT);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(transLimit.getLimit_type())) {
			sb.append( " and t1.limit_type = ? " );
			list.add(transLimit.getLimit_type());
		}	
		if (!StringUtils.isBlank(transLimit.getLimit_idx())) {
			sb.append( " and t1.limit_idx = ? " );
			list.add(transLimit.getLimit_idx());
		}	
		if (!StringUtils.isBlank(transLimit.getLimit_uuid())) {
			sb.append( " and t1.limit_uuid != ? " );
			list.add(transLimit.getLimit_uuid());
		}		
		List result=transLimitDao.queryForList(sb.toString(), list);
		if(result==null||result.size()==0){
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.ERROR;
	}
	
	public void initTransLimit(String channel_no,String trf_mchnt_cd,Map map){
		App_Trans_LimitEntity transLimit=new App_Trans_LimitEntity();
		transLimit.setLimit_uuid(IDFactoryUtil.getUUID());
		transLimit.setChannel_no(channel_no);
		transLimit.setLimit_amount(SystemConstant.LIMIT_AMOUNT);
		transLimit.setLimit_idx(trf_mchnt_cd);
		transLimit.setLimit_type(SystemConstant.LIMIT_TYPE_02);
		transLimit.setLimit_st(SystemConstant.LIMIT_ST_0);
		transLimit.setBegtime("000000");
		transLimit.setEndtime("235959");
		transLimit.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
		transLimit.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));					
		transLimit.setRec_st(SystemConstant.NORMAL_STATUS);
		transLimit.setLast_oper_in(SystemConstant.OPER_ADD);
		transLimit.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		transLimitDao.save(transLimit);
	}

}
